﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using YouHP.Model;
using YouHP.BLL;
using YouHP.SqlServerDAL;

namespace youhp_symptom_stat
{
    class Program
    {
        static int MaxSymptomNum = 10000;

        static void GenerateSymptomStats(int sid, string sFileName)
        {
            if (sid > 0)
            {
                GenerateSymptomStat(sid, sFileName);
            }
            else
            {
                int SymptomCount = 0;
                SymptomSearchConditon cond = new SymptomSearchConditon();
                List<SymptomInfo> SymptomDataList = SymptomBLL.Instance.GetSymptomList(MaxSymptomNum, 1, cond, out SymptomCount);
                foreach (SymptomInfo info in SymptomDataList)
                {
                    try
                    {
                        GenerateSymptomStat(info.Id, string.Format("{0}{1}.js", sFileName, info.Id));
                    }
                    catch (System.Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }

                }

            }
        }
        static bool GenerateSymptomStat(int sid,string sFileName)
        {
            List<SymptomStatSeverityCount> lst = SymptomBLL.Instance.GetSymptomStatSeverity(sid);
            Console.WriteLine(lst.Count);

            if (null == lst)
            {
                Console.WriteLine("get stat error");
                return false;
            }

            List<SymptomStatDiseaseCount> lstDisease = SymptomBLL.Instance.GetSymptomStatDisease(sid);
            if (null == lstDisease)
            {
                Console.WriteLine("get stat error");
                return false ;
            }
            using (StreamWriter os = new StreamWriter(sFileName))
            {
                os.WriteLine("var stat_radio=[];");
                int i = 0;
                foreach (SymptomStatSeverityCount stat in lst)
                {
                    os.WriteLine(string.Format("stat_radio[{0}]=['{1}',{2},{3}];", i++, stat.SeverityName, stat.PatientCount, stat.DegreeValue));
                }

                os.WriteLine("var treatment_stat=[];");
                i = 0;
                foreach (SymptomStatDiseaseCount stat in lstDisease)
                {
                    os.WriteLine(string.Format("treatment_stat[{0}]=['{1}',{2},'{3}'];", i++, stat.DiseaseName, stat.PatientCount, stat.DiseaseId));

                }
            }
            return true;
        }
        static void GenerateTreamentDosageStats(int sid, string sFileName)
        {
            if (sid > 0)
            {
                GenerateTreamentDosageStat(sid, sFileName);
            }
            else
            {
                int TreatmentCount = 0;
                TreatmentSearchCondition cond = new TreatmentSearchCondition();
                List<TreatmentInfo> TreatmentDataList = TreatmentBLL.Instance.GetTreatmentList(
                MaxSymptomNum, 1,
                cond, out TreatmentCount);
                foreach (TreatmentInfo info in TreatmentDataList)
                {
                    try
                    {
                        GenerateTreamentDosageStat(info.Id, string.Format("{0}{1}.js", sFileName, info.Id));
                    }
                    catch (System.Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }

                }

            }
        }
        static bool GenerateTreamentDosageStat(int tid, string sFileName)
        {
            TreatmentInfo Info = TreatmentBLL.Instance.GetTreatmentInfo(tid);
            if( null == Info)
            {
                Console.WriteLine("get treatment info error");
                return false;
            }
            List<TreatmentStatDosage> lst = TreatmentBLL.Instance.GetTreatmentStatDosage(tid);
            if (null == lst)
            {
                Console.WriteLine("get treatment dosage error");
                return false;
            }
            using (StreamWriter os = new StreamWriter(sFileName))
            {
                os.WriteLine("var treament_dosage=[];");
                int i = 0;
                foreach (TreatmentStatDosage stat in lst)
                {
                    os.WriteLine(string.Format("treament_dosage[{0}]=['{1}{2}/{3}',{4},{1}];", i++, stat.DataValue, Info.MeasurementUnits, Info.Frequency, stat.PatientCount));
                }
            }
            return true;
        }
        static void GenerateDiseaseStats(int did, string sFileName)
        {
            if (did > 0)
            {
                GenerateDiseaseStat(did, sFileName);
            }
            else
            {
                int DiseaseCount = 0;
                DiseaseSearchCondtion cond = new DiseaseSearchCondtion();
                List<DiseaseInfo> DiseaseDataList = DiseaseBLL.Instance.GetDiseaseList(
                MaxSymptomNum, 1,
                cond, out DiseaseCount);
                foreach (DiseaseInfo info in DiseaseDataList)
                {
                    try
                    {
                        GenerateDiseaseStat(info.Id, string.Format("{0}{1}.js", sFileName, info.Id));
                    }
                    catch (System.Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }

                }

            }

        }
        static bool GenerateDiseaseStat(int did, string sFileName)
        {
            List<DiseaseStatRegionInfo> lst = DiseaseBLL.Instance.GetDiseaseStatRegion(did, 0);
            List<DiseaseStatSymptomInfo> lst1 = DiseaseBLL.Instance.GetDiseaseStatSymptom(did);
            List<DiseaseStatTreamentInfo> lst2 = DiseaseBLL.Instance.GetDiseaseStatTreament(did);
            
            if (null == lst || null == lst1 || null == lst2)
            {
                Console.WriteLine("get disease error");
                return false;
            }
            using (StreamWriter os = new StreamWriter(sFileName))
            {
                os.WriteLine("var disease_region=[];");
                int i = 0;
                foreach (DiseaseStatRegionInfo stat in lst)
                {
                    os.WriteLine(string.Format("disease_region[{0}]=['{1}',{2},'{3}'];", i++, stat.RegionName, stat.PatientCount, stat.RegionId));
                }

                os.WriteLine("var disease_symptom=[];");
                i = 0;
                foreach (DiseaseStatSymptomInfo stat in lst1)
                {
                    os.WriteLine(string.Format("disease_symptom[{0}]=['{1}',{2},{3}];", i++, stat.SymptomName, stat.PatientCount, stat.SymptomId));
                }

                os.WriteLine("var disease_treatment=[];");
                i = 0;
                foreach (DiseaseStatTreamentInfo stat in lst2)
                {
                    os.WriteLine(string.Format("disease_treatment[{0}]=['{1}',{2},{3}];", i++, stat.TreatmentName, stat.PatientCount, stat.TreatmentId));
                }
            }
            return true;
        }

       

        
        //生成js文件
        static void Main(string[] args)
        {
            if( args.Length != 3 )
            {
                Console.WriteLine("write error");
                Console.Write("1  symptonid symptom_stat_");
                Console.Write("2 treamtid treatment_stat_");
                Console.Write("3 diseaseid disease_stat_");
                return;
            }
            Console.WriteLine(args[0]);
            int type = Convert.ToInt32(args[0]);
            if ( 1 == type)
            {
                int sid = Convert.ToInt32(args[1]);
                GenerateSymptomStats(sid, args[2]);                
            }
            else if ( 2 == type )
            {
                int tid = Convert.ToInt32(args[1]);
                GenerateTreamentDosageStats(tid, args[2]);     
            }
            else if ( 3 == type )
            {
                int did = Convert.ToInt32(args[1]);
                GenerateDiseaseStats(did, args[2]);
            }
        }
    }
}
